package xyz.xuanlee.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import xyz.xuanlee.springcloud.entities.CommonResult;
import xyz.xuanlee.springcloud.entities.Payment;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {

    public static final String PAYMENT_BASE_URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Integer> create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_BASE_URL + "/payment/create",
                payment,
                CommonResult.class);
    }

    @GetMapping("/consumer/payment/getById/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_BASE_URL + "/payment/getPayment/" + id,
                CommonResult.class);
    }

    @GetMapping("/consumer/payment/consul")
    public String paymentzk() {
        return restTemplate.getForObject(PAYMENT_BASE_URL + "/payment/consul", String.class);
    }
}
